package org.apache.dolphinscheduler.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.service.ApplyService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.Apply;
import org.apache.dolphinscheduler.dao.entity.Product;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.spi.enums.ResourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "超算资源 API")
@RestController
@RequestMapping("/apply")
public class ApplyController extends BaseController{

    @Autowired
    private ApplyService applyService;
    @PostMapping
    @ResponseStatus(HttpStatus.OK)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("申请资源")
    @PermissionCheck(permission = "csresources:application")
    public Result add (@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       @RequestBody Apply apply
                       ) {
        return applyService.add(loginUser, apply);
    }

    @PutMapping
    @ResponseStatus(HttpStatus.OK)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("审核")
    @PermissionCheck(permission = "csresources:audit")
    public Result audit (@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                         @RequestBody Apply apply
                         ) {
        int id = apply.getId();
        int type = apply.getApplyType();
        String account = apply.getApplyAccount();
        String remark = apply.getApplyComment();
        return applyService.audit(loginUser, id, type, account, remark);
    }

    @GetMapping
    @ApiOperation("列表")
    @PermissionCheck(permission = "csresources:detail")
    public Result list (@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                        @RequestParam(value = "pageSize") Integer pageSize,
                        @RequestParam(value = "pageNo") Integer pageNo,
                        @RequestParam(value = "type", required = false) String type,
                        @RequestParam(value = "searchVal", required = false) String searchVal,
                        @RequestParam(value = "applyAccount", required = false) String applyAccount
                        ) {
        return applyService.list(loginUser, pageSize, pageNo, type, searchVal,applyAccount);
    }

    @PutMapping(value="/save")
    @ApiOperation("保存token和pwd")
    public Result saveToken (@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                            @RequestBody Apply apply
                            ) {
        return applyService.saveToken(loginUser, apply);
    }

    @GetMapping(value="/get")
    @ApiOperation("通过id查询或是全体查询")
    @PermissionCheck(permission = "csresources:detail")
    public Result get (@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       @RequestParam(value = "id", required = false) Integer id
                       ) {
        return applyService.get(loginUser, id);
    }

}
